/*
 * Game1State.java
 *
 * Created on 2006. december 5., 19:21
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hu.de.game1;

import hu.de.ai.games.*;
import static hu.de.game1.Game1Operator.*;
import java.util.*;

public class Game1State extends GameState{
    
    private int[][] fields = new int[5][5];
    /** Creates a new instance of Game1State */
    public Game1State() {
        player = 1;
        for(int i = 0; i < 5; i++) {
            for(int j = 0; j < 5; j++) {
                fields[i][j] = 0;
                if(j == 0 || (j == 1 && (i == 0 || i == 4))) {
                    fields[i][j] = 1;
                } else if(j == 4 || (j == 3 && (i == 0 || i == 4))) {
                    fields[i][j] = -1;
                }
            }
        }
    }

    public Game1State(Game1State state) {
        player = -state.player;
        for(int i = 0; i < 5; i++) {
            for(int j = 0; j < 5; j++) {
                fields[i][j] = state.fields[i][j];
            }
        }
    }
    
    public boolean celfeltetel() {
        return nyert() != 0;
    }

    public int nyert() {
        if(fields[0][0] == -1 &&
                fields[1][0] == -1 &&
                fields[2][0] == -1 &&
                fields[3][0] == -1 &&
                fields[4][0] == -1 &&
                fields[0][1] == -1 &&
                fields[4][1] == -1) 
            return -1;
        if(fields[0][4] == 1 &&
                fields[1][4] == 1 &&
                fields[2][4] == 1 &&
                fields[3][4] == 1 &&
                fields[4][4] == 1 &&
                fields[0][3] == 1 &&
                fields[4][3] == 1) 
            return 1;
        return 0;
    }

    public int heuristics() {
        int h = 0;
        if(fields[0][0] == -1) h--;
        if(fields[1][0] == -1) h--;
        if(fields[2][0] == -1) h--;
        if(fields[3][0] == -1) h--; 
        if(fields[4][0] == -1) h--;
        if(fields[0][1] == -1) h--;
        if(fields[4][1] == -1) h--;
        if(fields[0][4] == 1) h++;
        if(fields[1][4] == 1) h++;
        if(fields[2][4] == 1) h++;
        if(fields[3][4] == 1) h++;
        if(fields[4][4] == 1) h++;
        if(fields[0][3] == 1) h++;
        if(fields[4][3] == 1) h++;
        return h * player;
    }

    public List<GameOperator> usableOperators() {
        List <GameOperator> operators = new LinkedList<GameOperator>();
        for(int column = 0; column < 4; column++) {
            for(int row = 0; row < 4; row++) {
                for(int direction = 0; direction < 3; direction++) {
                    Game1Operator operator = new Game1Operator(direction, column, row);
                    if(operator.isUsable(this)) operators.add(operator);
                }
            }
        }
        return operators;
    }

    public GameState createState(GameOperator operator) {
        Game1Operator op = (Game1Operator) operator;
        Game1State newState = new Game1State(this);
        int curplayer = getField(op.column, op.row);
        newState.fields[op.column][op.row] = 0;
        int targetFieldColumn = op.column;
        int targetFieldRow = op.row;
        if(op.direction == DIR_NE || op.direction == DIR_NW)
            targetFieldRow--;
        else
            targetFieldRow++;
        if(op.direction == DIR_SE || op.direction == DIR_NE)
            targetFieldColumn++;
        else
            targetFieldColumn--;
        newState.fields[targetFieldColumn][targetFieldRow] = curplayer;
        newState.player = -player;
        return newState;
    }
    
    public int getField(int column, int row) { return fields[column][row]; }

    public String toString() {
        StringBuffer buffer = new StringBuffer("\t 12345\n");
        for(int i = 0; i < 5; i++) {
            buffer.append('\t').append(i + 1);
            for(int j = 0; j < 5; j++) {
                switch (fields[j][i]) {
                    case -1: buffer.append('K'); break;
                    case 1: buffer.append('S'); break;
                    case 0: buffer.append(' '); break;
                }
            }
            buffer.append('\n');
        }
        return buffer.toString();
    }
}
